﻿using System.Web;

namespace PageAdmin.Utils
{
    /// <summary>
    /// 获取HttpContext.Current.Request.Url有关的参数
    /// </summary>
    public class RequestUrlHelper
    {
        /// <summary>
        /// 获取当前请求的域名，如：www.pageadmin.net
        /// </summary>
        /// <returns></returns>
        public static string GetHost()
        {
            return HttpContext.Current.Request.Url.Host;
        }

        /// <summary>
        /// 获取当前请求的的端口号
        /// </summary>
        /// <returns></returns>
        public static int GetPort()
        {
            return HttpContext.Current.Request.Url.Port;
        }

        /// <summary>
        /// 判断是否是https协议。
        /// </summary>
        /// <returns></returns>
        public static bool IsHttps()
        {
            return HttpContext.Current.Request.Url.ToString().ToLower().Contains("https://");
        }


        /// <summary>
        /// 获取当前请求的网站完整的Domain
        /// </summary>
        /// <param name="appendTrailingSlash">是否斜杠结尾，默认true</param>
        /// <returns></returns>
        public static string GetDomain(bool appendTrailingSlash=true)
        {
            string domain = "";
            var uri = HttpContext.Current.Request.Url;
            if (uri.ToString().ToLower().Contains("https://"))
            {
                domain = "https://";
            }
            else
            {
                domain = "http://";
            }
            domain += uri.Host;
            int port = uri.Port;
            if (port != 80 && port != 443)
            {
                domain += ":" + port.ToString();
            }
            if(appendTrailingSlash)
            {
                domain += "/";
            }
            return domain;
        }

    }
}
